<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="workList" @selection-change="handleSelectionChange" style="width: 100%" height="500" border :row-class-name="tableRowClassName">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="项目名称" align="center" prop="projectName" fixed="left"/>
      <el-table-column label="责任人" align="center" prop="user.nickName" width="80"/>
      <el-table-column label="工作内容" align="center" prop="workContent" fixed="left"/>
      <el-table-column label="审核人" align="center" prop="checkUserName" width="80"/>
      <el-table-column label="开始时间" align="center" prop="startTime" width="95">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="finishTime" width="95">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="realFinishTime" width="95">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.realFinishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属组别" align="center" prop="workUserGroup" width="80"/>
      <el-table-column label="工作时长" align="center" prop="fileHours" width="40"/>
      <el-table-column label="是否输出" align="center" prop="isSubmitFile" :formatter="isSubmitFileFormat" width="40"/>
      <el-table-column label="输出物名" align="center" prop="submitName" width="120"/>
      <el-table-column label="是否超时" align="center" prop="isTimeOut" :formatter="isTimeOutFormat" width="40"/>
      <el-table-column label="状态" align="center" prop="state" :formatter="stateFormat" width="80"/>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 上传文件 -->
     <el-dialog :title="upload.title" :visible.sync="upload.open" :close-on-click-modal="false"  width="800px" append-to-body>
      <el-tabs v-model="upload.activeName">
        <el-tab-pane id="fileUpload" label="上传" name="first" v-hasPermi="['myjob:waitwork:uploadFile']">
              <el-upload ref="upload" 
              :headers="upload.headers" 
              :limit="1" 
              :data="upload.data"
              :action="upload.url" 
              :disabled="upload.isUploading" 
              :on-progress="handleFileUploadFile" 
              :on-success="handleFileSuccess" 
              :auto-upload="false" drag>
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">
                将文件拖到此处，或
                <em>点击上传</em>
              </div>
            </el-upload>
          <el-button type="primary" @click="submitFileForm" style="display:block;margin:0 auto;marginTop:1em">上 传</el-button>
        </el-tab-pane>
        <el-tab-pane label="附件查看" name="second">
            <el-row :gutter="10" class="mb8">
            </el-row>
            <el-table v-loading="loading" :data="upload.fileLists" border @selection-change="handleSelectionFileChange">
                  <el-table-column type="selection" width="55" align="center" />
                  <el-table-column label="文件ID" align="center" prop="id" v-if="false"/>
                  <el-table-column label="文件URL" align="center" prop="url" v-if="false"/>
                  <el-table-column label="文件名称" align="center" prop="name"/>
                  <el-table-column label="文件类型" align="center" prop="type" :formatter="stateFileTypeFormat" />
                  <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                      <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        v-hasPermi="['myjob:waitwork:downFile']"
                        @click="handleFileDownload(scope.row)"
                      >下载</el-button>
                      <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleFileDelete(scope.row)"
                        v-hasPermi="['myjob:waitwork:deleFile']"
                      >删除</el-button>
                    </template>
                  </el-table-column>
            </el-table>
            <pagination
              v-show="upload.total>0"
              :total="upload.total"
              :page.sync="upload.queryParams.pageNum"
              :limit.sync="upload.queryParams.pageSize"
              @pagination="getList"
            />
        </el-tab-pane>
        <el-tab-pane label="相关附件" name="third">
            <el-row :gutter="10" class="mb8">
              <el-table v-loading="loading" :data="upload.relevantFile" border @selection-change="handleSelectionFileChange">
                  <el-table-column type="selection" width="55" align="center" />
                  <el-table-column label="文件ID" align="center" prop="id" v-if="false"/>
                  <el-table-column label="文件URL" align="center" prop="url" v-if="false"/>
                  <el-table-column label="文件名称" align="center" prop="name"/>
                  <el-table-column label="文件类型" align="center" prop="type" :formatter="stateFileTypeFormat" />
                  <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                      <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        v-hasPermi="['myjob:waitwork:downFile']"
                        @click="handleFileDownload(scope.row)"
                      >下载</el-button>
                      <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleFileDelete(scope.row)"
                        v-hasPermi="['myjob:waitwork:deleFile']"
                      >删除</el-button>
                    </template>
                  </el-table-column>
            </el-table>
            </el-row>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitFileForm">确 定</el-button> -->
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    
  </div>
</template>

<script>
import { listProject} from "@/api/statistics/work/work.js";
import { FileAllList } from "@/api/workflow/apply";
import Editor from '@/components/Editor';
import Treeselect from "@riophae/vue-treeselect";
import {getUserTree} from "@/api/trialproduce/car";
import {getToken } from "@/utils/auth";
import { selectFiles,deleteFiles } from "@/api/work/user";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "User",
  components: { Treeselect,Editor },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务分配表格数据
      workList: [],
      //文件类型
      textFileTypeOptions: [],
      //是否输出
      isSubmitFileOptions:[],
      //状态
      stateOptions: [],
      // 是否超时字典
      isTimeOutOptions: [],
      //用户数结构
      userTree:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workId: null,
        userId: null,
        workHours: null,
        fileHours: null,
        fileId: null,
        textHours: null,
        workUserGroup: null,
        state: null,
        isTimeOut: null,
        startTime: null,
        finishTime: null,
        realFinishTime: null,
        workContent: null,
        deptId: null,
        isSubmitFile: null,
        submitName: null,
        workType: null,
        checkUserId: null
      },
      apply: {
        title:"",
        open: false,
        form:{
          tableId:"",
          tableName:"0",
          checkList:[]
        }
      },
      //审核人ID
      checkUserId: null,
      //审核流程
      progresses: [],
       //基础路径
      baseURL : process.env.VUE_APP_BASE_API,
      //文件类型
      textFileTypeOptions: [],
      //工作类型
      workTypeOptions: [],
      upload: {
        title: null,
        open: false,
        // 上传标签页
        activeName: 'first',
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken()},
        // 是否禁用上传
        isUploading: false,
        // 上传的地址
        url: process.env.VUE_APP_BASE_API+"/work/process/user/upload",
        //路径
        baseURL:  process.env.VUE_APP_BASE_API,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken()},
        data: {
          tableId: null
        },
        //总数
        total: 0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        relevantFile: [{
          id:'',
          url: "",
          name: '',
          type: ''
        }],
        fileLists: [{
          id:'',
          url: "",
          name: '',
          type: ''
        }],
      },
    };
  },
  props:{
    queryWorkParam: {
      type: Object,
      required: false
    }
  },
  created() {
    // console.log("项目："+JSON.stringify(this.queryWorkParam));
    this.queryParams = this.queryWorkParam
    this.getList();
    this.handleUserTree();
    this.getDicts("sys_yes_no").then(response => {
      this.isTimeOutOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isSubmitFileOptions = response.data;
    });
    this.getDicts("work_file_state").then(response => {
      this.stateOptions = response.data;
    });
    this.getDicts("sys_file_type").then(response => {
      this.textFileTypeOptions = response.data;
    });
    this.getDicts("wf_work_type").then(response => {
      this.workTypeOptions = response.data;
    });
    
  },
  methods: {
    //用户树
    handleUserTree(){
      getUserTree().then(response => {
        this.userTree = response.data;
      });
    },
    /** 查询任务分配列表 */
    getList() {
      this.loading = true;
      listProject(this.queryWorkParam).then(response => {
        this.workList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 文件类型翻译
    stateFileTypeFormat(row, column) {
      return this.selectDictLabel(this.textFileTypeOptions, row.type);
    },
    // 是否提交输出物字典翻译
    isSubmitFileFormat(row, column) {
      return this.selectDictLabel(this.isSubmitFileOptions, row.isSubmitFile);
    },
    // 是否超时字典翻译
    isTimeOutFormat(row, column) {
      return this.selectDictLabel(this.isTimeOutOptions, row.isTimeOut);
    },
    // 任务状态翻译
    stateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions, row.state);
    },
    // 工作类型翻译
    workTypeFormat(row, column) {
      return this.selectDictLabel(this.workTypeOptions, row.workType);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.itemId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有任务分配数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUser(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    //修改列颜色
    tableRowClassName({row, rowIndex}) {
        if (row.isTimeOut === 'Y') {
          return 'danger-row';
        }
    },
    
    resetApplyForm(){
      this.apply.form ={
          checkList:[
            {
                checkUserId: null
            }
          ]
        }
    },
    resetProcess(){
      this.progresses = [
      ]
    },
    //监听用户树选择
    userTreeFileHoursList(value){
       if(this.progresses.length<=1){
          this.$message({
            showClose: true,
            message: '请添加审核流程！',
            type: 'warning'
          });
          return;
      }else{
        this.progresses[this.progresses.length-1].date = value.label.substring(0,value.label.lastIndexOf("-"));
        this.progresses[this.progresses.length-1].id = value.id;
      }
    },
    /** 附件上传按钮操作 */
    handleFileUpload(row) {
      const itemId = row.itemId;
      this.upload.title = "上传附件";
      this.upload.open = true;
      this.upload.data.tableId = itemId;
      this.getFileList();
    },
    // 附件上传中处理
    handleFileUploadFile(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 附件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = true;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "上传结果", { dangerouslyUseHTMLString: true });
      this.getFileList();
      this.getList();
    },
    // 附件提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
     //附件列表
    handleSelectionFileChange(selection){
      this.id = selection.map(item => item.id)
      this.uri = selection.map(item => item.uri)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查询附件列表 */
    getFileList() {
      this.loading = true;
      console.log('附件列表', this.upload.data.tableId)

      FileAllList(this.upload.data.tableId).then((response) => {
        console.log('附件列表', response)
        this.upload.fileLists = response.data.itemFile;
        this.upload.relevantFile = response.data.relevantFile;
        // this.upload.total = response.total;
        this.loading = false;
      });
    },
    // 删除文件操作
    handleFileDelete(row){
      const id = row.id || this.id;
      this.$confirm('是否确认删除任务分配编号为"' + id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return deleteFiles(id);
        }).then(() => {
          this.getFileList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    // 下载文件
    handleFileDownload(row){
      const id = row.id || this.id;
      const url = row.uri || this.uri;
      window.location.href = this.baseURL+url;
    },
  }
};
</script>
<style>
.el-upload-list__item-name{
    font-size: 16px;
}
#fileUpload .el-upload-dragger{
  display:block;
  margin:0 auto;
  margin-top:1em;
  width: 760px;
  height: 180px;
}
.el-table .warning-row {
  background: rgb(248, 226, 186);
}

.el-table .success-row {
  background: #ffffff;
}

.el-table .danger-row {
  background: #f8b690;
}
</style>